package com.hyg.lib_common.ui.adpter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hyg.lib_common.R;
import com.hyg.lib_common.databinding.ItemBluetoothListBinding;

import java.util.ArrayList;


/**
 * @author: Administrator
 * @date: 2021/2/24
 */
public class BlueToothUnconnectListAdapter extends RecyclerView.Adapter<BlueToothUnconnectListAdapter.ViewHolder> {
    Context context;
    ArrayList<BluetoothDevice> list;
    ListClickListener listener;

    public BlueToothUnconnectListAdapter(Context context, ArrayList<BluetoothDevice> list) {
        this.context = context;
        this.list = list;
    }

    public void setOnItemClickListener(ListClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bluetooth_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.binding.ivIcon.setImageResource(R.mipmap.icon_zhentou);
        holder.binding.tvName.setText(list.get(position).getName());
        holder.binding.tvInfo.setText(list.get(position).getBondState() + "&&" + list.get(position).getAddress());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onItemClicked(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ItemBluetoothListBinding binding;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
          binding=ItemBluetoothListBinding.bind(itemView);
        }
    }

    public interface ListClickListener {
        void onItemClicked(int position);
    }

}
